﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Portal.Web.Areas.Admin.Controllers
{
    public class RecruitmentController : BaseAdminController
    {
        private readonly IRecruitmentRepository _recruitmentRepository;

        public RecruitmentController(IRecruitmentRepository recruitmentRepository)
        {
            _recruitmentRepository = recruitmentRepository;
        }

        // GET: /Admin/Recruitment/

        public ActionResult Index()
        {
            _recruitmentRepository.Get();
            return View();
        }

        public ActionResult GetListJson(string sort = "Sort", string order = "DESC", int page = 1, int rows = 10)
        {
            return Json(_recruitmentRepository.Get().Sorting(sort, order).JqEasyuiDataGrid(page, rows));
        }
        public ActionResult Create()
        {
            var model = _recruitmentRepository.CreateEntity();
            return View(model);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection formCollection, Recruitment entity)
        {
            var model = _recruitmentRepository.CreateEntity();
            model.Name = entity.Name;
            model.Culture = Culture;
            model.Founder = CurrentUserName;
            model.Number = entity.Number;
            model.Degree = entity.Degree;
            model.Age = entity.Age;
            model.WorkExperience = entity.WorkExperience;
            model.WayFor = entity.WayFor;
            model.WorkType = entity.WorkType;
            model.Workplace = entity.Workplace;
            model.QQ = entity.QQ;
            model.Email = entity.Email;
            model.Telnet = entity.Telnet;
            model.Contact = entity.Contact;
            model.OtherTreatment = entity.OtherTreatment;
            model.MonthlySalaryRange = entity.MonthlySalaryRange;
            model.Sex = entity.Sex;
            model.Language = entity.Language;
            model.AssessmentMethod = entity.AssessmentMethod;
            model.Validity = entity.Validity;
            _recruitmentRepository.Add(model);
            _recruitmentRepository.Save();
            ViewData["Result"] = new AjaxResult { message = "添加成功", state = false };
            return View(model);
        }

        public ActionResult Edit(string id)
        {
            var model = _recruitmentRepository.Get().SingleOrDefault(o => o.Id == id);
            if (model == null)
                return NotFount(new AjaxResult { message = "没有找到记录", state = false });
            return View(model);
        }
        [HttpPost]
        public ActionResult Edit(Recruitment entity)
        {
            var model = _recruitmentRepository.Get().SingleOrDefault(o => o.Id == entity.Id);
            if (model == null)
                return NotFount(new AjaxResult { message = "没有找到记录", state = false });
            model.Name = entity.Name;
            model.Culture = Culture;
            model.Founder = CurrentUserName;
            model.Number = entity.Number;
            model.Degree = entity.Degree;
            model.Age = entity.Age;
            model.WorkExperience = entity.WorkExperience;
            model.WayFor = entity.WayFor;
            model.WorkType = entity.WorkType;
            model.Workplace = entity.Workplace;
            model.QQ = entity.QQ;
            model.Email = entity.Email;
            model.Telnet = entity.Telnet;
            model.Contact = entity.Contact;
            model.OtherTreatment = entity.OtherTreatment;
            model.MonthlySalaryRange = entity.MonthlySalaryRange;
            model.Sex = entity.Sex;
            model.Language = entity.Language;
            model.AssessmentMethod = entity.AssessmentMethod;
            model.Validity = entity.Validity;
            _recruitmentRepository.Update(model);
            _recruitmentRepository.Save();
            ViewData["Result"] = new AjaxResult { message = "修改成功", state = false };
            return View(model);
        }
        [HttpPost]
        public ActionResult Delete(List<string> id, FormCollection collection)
        {

            var list = _recruitmentRepository.Get().Where(o => id.Contains(o.Id));
            list.ForEach(o => _recruitmentRepository.Delete(o));
            _recruitmentRepository.Save();
            return Json(new AjaxResult { state = true, message = "删除成功" });
        }
    }
}
